/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ineatconseil.yougo.wicket;

import com.ineatconseil.yougo.dto.UserDto;
import com.ineatconseil.yougo.wicket.dao.UsersDao;
import org.apache.wicket.Request;
import org.apache.wicket.Session;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.authorization.strategies.role.Roles;

/**
 *
 * @author nicolasger
 */
public class YougoSession extends AuthenticatedWebSession {

	private static final long serialVersionUID = 1L;
	private UserDto user;

	public UserDto getUser() {
		return user;
	}
	
	public static YougoSession get() {
		return (YougoSession) Session.get();
	}

	public YougoSession(Request request) {
		super(request);
	}

	@Override
	public boolean authenticate(String username, String password) {
		user = UsersDao.getInstance().signin(username, password);
		return user !=null;
	}

	@Override
	public Roles getRoles() {
		if (isSignedIn()) {
			Roles roles = new Roles(Roles.USER);
			if(user.getAdmin())
				roles.add(Roles.ADMIN);
			return roles;
		}
		return null;
	}
	
}